package com.ruoyi.project.system.main.controller;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.RandomUtils;
import com.ruoyi.project.system.dept.domain.Dept;
import com.ruoyi.project.system.dept.service.IDeptService;
import com.ruoyi.project.system.detail.domain.ScoreDetail;
import com.ruoyi.project.system.detail.service.IScoreDetailService;
import com.ruoyi.project.system.main.domain.EchartsDto;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.main.domain.ScoreMain;
import com.ruoyi.project.system.main.service.IScoreMainService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;

/**
 * 得分主Controller
 * 
 * @author ruoyi
 * @date 2021-02-22
 */
@Controller
@RequestMapping("/system/scoreMain")
public class ScoreMainController extends BaseController
{
    private String prefix = "system/main";

    @Autowired
    private IUserService userService;

    @Autowired
    private IScoreMainService scoreMainService;

    @Autowired
    private IScoreDetailService scoreDetailService;

    @Autowired
    private IDeptService deptService;

    @RequiresPermissions("system:main:view")
    @GetMapping()
    public String main()
    {
        return prefix + "/main";
    }

    /**
     * 查询得分主列表
     */
    @RequiresPermissions("system:main:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ScoreMain scoreMain)
    {
        startPage();
        List<ScoreMain> list = scoreMainService.selectScoreMainList(scoreMain);
        return getDataTable(list);
    }


    /**
     * 新增得分主
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap)
    {
        List<User> userList = userService.selectUserList(new User());
        modelMap.put("userList",userList);
        return prefix + "/add";
    }

    /**
     * 新增保存得分主
     */
    @RequiresPermissions("system:main:add")
    @Log(title = "得分主", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ScoreMain scoreMain, ScoreDetail scoreDetail)
    {
        //自动生成得分批号
        scoreMain.setScoreNumber(RandomUtils.verifyUserName(4,4));
        //保证不为null 方便总分加减
        scoreMain.setTotalScore(0L);
        //先保存主表内容来获取 主表id
        scoreMainService.insertScoreMain(scoreMain);
        //封装scoreMain的id到scoreDetail 中
        scoreDetail.setScoreMainId(scoreMain.getId());
        //每次记入一次 会给总分加上该分
        Long score = scoreDetail.getScore();
        //更新主表的总分
        scoreMainService.updateScoreMainTotalScore(scoreMain.getUserId(),score);

        return toAjax(scoreDetailService.insertScoreDetail(scoreDetail));
    }

    /**
     * 修改得分主
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {


        ScoreMain scoreMain = scoreMainService.selectScoreMainById(id);
        //查询用户
        User user = userService.selectUserById(scoreMain.getUserId());
        //查询组织
        Dept dept = deptService.selectDeptById(scoreMain.getDeptId());
        mmap.put("scoreMain", scoreMain);
        mmap.put("dept", dept);
        mmap.put("user", user);
        return prefix + "/edit";
    }

    /**
     * 修改保存得分主
     */
    @RequiresPermissions("system:main:edit")
    @Log(title = "得分主", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ScoreMain scoreMain)
    {
        return toAjax(scoreMainService.updateScoreMain(scoreMain));
    }

    /**
     * 删除得分主
     */
    @RequiresPermissions("system:main:remove")
    @Log(title = "得分主", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        ScoreMain scoreMain = scoreMainService.selectScoreMainById(Long.valueOf(ids));
        Long userId = scoreMain.getUserId();

        ScoreMain scoreMain2 = new ScoreMain();
        scoreMain2.setUserId(userId);
        List<ScoreMain> scoreMainList = scoreMainService.selectScoreMainAll(scoreMain2);
        //去详细表删除该网格员的所有信息
        String scoreMainIds = "";
        for (ScoreMain main : scoreMainList) {
            scoreMainIds += main.getId()+",";
        }
        scoreDetailService.deleteScoreDetailByscoreMainIds(scoreMainIds);

        return toAjax(scoreMainService.deleteScoreMainByIds(scoreMainIds));
    }

    /**
     * 提供Echarts需要的数据
     * @return
     */
    @GetMapping("/selectEchartsData")
    @ResponseBody
    public AjaxResult selectEchartsData(){
        //用于封装返回的数据
        Map<String, Object> hashMap = new HashMap<>();
        //查询该区的街道
        List<Dept> streetList =  deptService.selectAllStreet();
        List<String> streets = new LinkedList<>();
        if(null!=streetList && streetList.size()>0){
            streetList.forEach((val)->{
                streets.add(val.getDeptName());
            });
        }
        System.out.printf(streets.toString());
        hashMap.put("streets",streets);

        List<EchartsDto> echartsDtoList =  scoreMainService.selectScoreByEcharts();
        System.out.printf(echartsDtoList.toString());
        if (null != echartsDtoList && echartsDtoList.size()>0){

        }


        return  AjaxResult.success(hashMap);

    }



}
